#include"PL1167.h"
extern  BitStatus  Init_err;
u8 RegH;
u8 RegL;
u8 Sync_Flag=0;
extern u8   num_id,Speed_class,mode;
//0x18c0	0x19e0
/*
REG9	REG26	Measured	Pout
0x4800	0x1be0	3.8dB	4.3dB
0x4800	0x19e0	3.5dB	4.0dB
0x1800	0x1be0	2.9dB	3.4dB
0x1840	0x19e0	2.3dB	2.8dB
0x18c0	0x19e0	1.7dB	2.2dB
0x1940	0x19e0	1.0dB	1.5dB
0x19c0	0x19e0	0.3dB	0.8dB
0x1a40	0x19e0	-0.5dB	0dB
0x1ac0	0x19e0	-1.2dB	-0.7dB
*/
const u8 RF_TS_TX[] = 
{
	0,0X6F,0XE0,
	1,0X56,0X81,
	2,0x66,0X17,
	4,0X9C,0XC9,
	5,0X66,0X37,
	7,0X00,0X4C,
	8,0X6C,0X90,//0X91
	9,0X48,0X00,
	//10,0x7f,0xfd,
	11,0x00,0x08,
	//12,0x00,0x00,
	13,0x48,0xbd,//0xdb
	22,0x00,0xff,
	//23,0x80,0x05,
	24,0x00,0x67,
	//25,0x16,0x59,
	26,0x19,0xe0,
	27,0x13,0x00,
	//28,0x13,0x00,
	32,0x48,0x00,
	33,0x3f,0xc7,
	34,0x20,0x00,
	35,0x03,0x80,
	//???
	36,0x14,0x7A,
	//37,0x86,0x75,
	//38,0x9a,0x0b,
	39,0x25,0x8B,
	//???
	
	40,0x44,0x02,
	41,0xb0,0x00,
	42,0xfd,0xb0,
//	43,0x00,0x0f,
                                                                    
0xff
};


const u8 RF_TS[] =

{
	0,0X6F,0XE0,
	1,0X56,0X81,
	2,0x66,0X17,
	4,0X9C,0XC9,
	5,0X66,0X37,
	7,0X00,0X4C,
	8,0X6C,0X90,//0X91
	9,0X48,0X00,
	10,0x7f,0xfd,
	11,0x00,0x08,
	12,0x00,0x00,
	13,0x48,0xbd,//0xdb
	22,0x00,0xff,
	23,0x80,0x05,
	24,0x00,0x67,
	25,0x16,0x59,
	26,0x19,0xe0,
	27,0x13,0xc0,
	28,0x18,0x00,
	32,0x48,0x00,
	33,0x3f,0xc7,
	34,0x20,0x00,
	35,0x03,0x80,
	//???
	36,0x14,0x7a,
	//37,0x86,0x75,
	//38,0x9a,0x0b,
	39,0x25,0x8b,
	//???
	
	40,0x44,0x02,
	41,0xb0,0x00,
	42,0xfd,0xb0,
	43,0x00,0x0f,
                                                                    
0xff
};

//=====================================
void Delay_us(u8  time)

{
     u8 j;
    for(j = 0;j < time;j++)
    {
      nop;
      nop;
     // nop();
     // nop();
     // nop();
    }
}

void WriteSpi(u8 Val)
{
  u8   Count_H;
  for(Count_H=0;Count_H<8;Count_H++)
  {
		 SPI_CLK_1;
		if(Val&0x80)
		{
				SPI_MOSI_1;
		}
		else
		{
				SPI_MOSI_0;
		}
		Val=Val<<1;
		nop;nop;nop;nop;
		SPI_CLK_0;
	}	

}
u8  ReadSpi(void)
{
	u8   Count_H,DATA;
	for(Count_H=0;Count_H<8;Count_H++)
	{
		SPI_CLK_1;
		nop;//nop();nop();nop();
		SPI_CLK_0;
		DATA=DATA<<1;	
		if(SPI_MISO)
		{
			DATA=DATA|1;
		}
		
	}	
	return   DATA;
}
void ReadReg(u8 reg)
{
	SPI_CS_0;
	Delay_us(4);
	WriteSpi(reg+0x80);
	nop;//nop();nop();nop();
	RegH=ReadSpi();
	nop;//nop();nop();nop();
	RegL=ReadSpi();
	SPI_CS_1;	
}


void WriteReg(u8 reg, u8 H, u8 L)
{
		SPI_CS_0;
		nop;//nop();nop();nop();
		WriteSpi(reg);
		nop;//nop();nop();nop();
		WriteSpi(H);
		nop;//nop();nop();nop();
		WriteSpi(L);
		SPI_CS_1;
		nop;//nop();nop();nop();		
}



void Pl1167_Init(void)
{
  u8 i;
  SPI_CS_1;
  Delay_us(30);				
  RF_RST_0;
  for(i = 0;i < 100;i++)
  {
      Delay_us(200);
  }	
  RF_RST_1;
  SPI_CLK_1;
  for(i = 0;i < 150;i++)
  {
      Delay_us(200);
  }
  for(i = 0;i < 255;i+=3)  
  {
    if(RF_TS[i] == 0xff) 
    {
             break;
    }													
    WriteReg(RF_TS[i],RF_TS[i+1],RF_TS[i+2]); 
  }	
  for(i = 0;i < 100;i++)
  {
      Delay_us(50);
  }									
}



BitStatus ReadReg_Init(void)
{	
	BitStatus     Init_err=RESET;
	ReadReg(0);
	if ((RegH!=0x6f)||(RegL!=0xE0))
	{
		Init_err=SET;
	}
	ReadReg(1);
	if ((RegH!=0x56)||(RegL!=0x81))
	{
		Init_err=SET;
	}
	ReadReg(2);
	if ((RegH!=0x66)||(RegL!=0x17))
	{
		Init_err=SET;
	}
	ReadReg(4);
	if ((RegH!=0x9C)||(RegL!=0xC9))
	{
		Init_err=SET;
	}
	ReadReg(5);
	if ((RegH!=0x66)||(RegL!=0x37))
	{
		Init_err=SET;
	}

	ReadReg(36);
	if ((RegH!=0x42)||(RegL!=0x31))
	{
		Init_err=SET;
	}
	ReadReg(37);
	if ((RegH!=0x86)||(RegL!=0x75))
	{
		Init_err=SET;
	}
	ReadReg(38);
	if ((RegH!=0x9a)||(RegL!=0x0b))
	{
		Init_err=SET;
	}
	ReadReg(39);
	if ((RegH!=0xde)||(RegL!=0xcf))
	{
		Init_err=SET;
	}
	SPI_CS_1;
        return  Init_err;
}

void PL1167_Send(u8 NUM,u8 *TXD_BUFF,u8 Ch_NO)
{
	
}

void PL1167_REC(u8 *BF)
{
	u8  CK,II;
	ReadReg(48);	
	SPI_CS_0;
	if((!(RegH&0X80))&&(!(RegH&0X40)))
	{	
		WriteSpi(50+0x80);
		Delay_us(1);
		CK=ReadSpi();
		if(CK==8)
		{
			for(II=0;II<CK;II++)
			{
				BF[II]=ReadSpi();
			}
		}
		SPI_CS_1;
	}
	WriteReg(7, 0x00,0x30);	
					 
}

void	RXD_Init(u8 Ch_NO)
{
	SPI_CS_0;
	Delay_us(3);
	WriteReg(7,0,0);
	Delay_us(2);
	WriteReg(52,0x80,0x80);
	Delay_us(2);
	WriteReg(7,0X00,0x80+Ch_NO);
	Delay_us(4);
}